﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace B13_Ex02_David_300512340_Alexey_321203689
{
    public partial class MainForm : Form
    {
        private MainLogic m_FacebookOperator = MainLogic.GetInstance();
        private AlbumWrapper m_SelectedAlbum;
        private ValuesToSearch m_ValuesToSearch;
        private string m_SelectedFriendName = string.Empty;

        public MainForm()
        {
            InitializeComponent();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            ApplicationSettings.Instance.LastWindowState = this.WindowState;
            ApplicationSettings.Instance.LastWindowSize = this.Size;
            ApplicationSettings.Instance.LastWindowLocation = this.Location;
            ApplicationSettings.Instance.AutoLogin = this.checkBoxAutoLogin.Checked;

            if (!this.checkBoxAutoLogin.Checked)
            {
                ApplicationSettings.Instance.AccessToken = string.Empty;
            }

            ApplicationSettings.Instance.Save();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Size = ApplicationSettings.Instance.LastWindowSize;
            this.WindowState = ApplicationSettings.Instance.LastWindowState;
            this.Location = ApplicationSettings.Instance.LastWindowLocation;
            if(string.IsNullOrEmpty(ApplicationSettings.Instance.AccessToken))
            {
                this.checkBoxAutoLogin.Checked = false;
            }
            else
            {
                this.checkBoxAutoLogin.Checked = true;
                autoLogin();
            }
        }

        private void autoLogin()
        {
            m_FacebookOperator.AutoLogIn();
            if (m_FacebookOperator.LoggedInUser != null)
            {
                fetchUserInfo();
            }
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            loginToFacebook();
        }

        private void loginToFacebook()
        {
            m_FacebookOperator.Login();
            if (m_FacebookOperator.LoggedInUser != null)
            {
                fetchUserInfo();
            }
        }

        private void fetchUserInfo()
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                labelName.Text = m_FacebookOperator.LoggedInUser.ToString();
                pictureBoxProfile.Load(m_FacebookOperator.LoggedInUser.ProfilePicture);
                listBoxFriends.DataSource = m_FacebookOperator.LoggedInUser.GetFriendsList();
                listBoxAlbums.DataSource = m_FacebookOperator.LoggedInUser.GetAlbumsList();
                listBoxEvents.DisplayMember = "Name";
                listBoxEvents.DataSource = m_FacebookOperator.LoggedInUser.GetEventsList();
                listBoxFeed.DataSource = m_FacebookOperator.LoggedInUser.GetFeedsList();
            }
        }

        private void listBoxFriends_SelectedIndexChanged(object sender, EventArgs e)
        {
            displaySelectedFriend();
        }

        private void displaySelectedFriend()
        {
            if (listBoxFriends.SelectedItems.Count == 1)
            {
                UserWrapper selectedFriend = listBoxFriends.SelectedItem as UserWrapper;

                if (selectedFriend.UserInformation.PictureNormalURL != null)
                {
                    m_SelectedFriendName = selectedFriend.ToString();
                    pictureBoxFriend.LoadAsync(selectedFriend.ProfilePicture);
                    listBoxAlbums.DataSource = selectedFriend.GetAlbumsList(); // To display each friend album list
                }
                else
                {
                    pictureBoxFriend.Image = pictureBoxFriend.ErrorImage;
                }
            }
        }

        private void listBoxEvents_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxEvents.SelectedItems.Count == 1)
            {
                Event selectedEvent = listBoxEvents.SelectedItem as Event;

                pictureBoxEvent.LoadAsync(selectedEvent.PictureNormalURL);
            }
        }

        private void linkLabelNewFeed_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                listBoxFeed.DataSource = m_FacebookOperator.LoggedInUser.GetFeedsList();
            }
        }

        private void buttonPost_Click(object sender, EventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                m_FacebookOperator.LoggedInUser.UserInformation.PostStatus(textBoxStatus.Text);
                MessageBox.Show(null, "Done posting status.", "Post status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxStatus.Clear();
            }
        }

        private void listBoxAlbums_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxAlbums.SelectedItems.Count == 1)
            {
                m_SelectedAlbum = listBoxAlbums.SelectedItem as AlbumWrapper;
                try
                {
                    pictureBoxAlbumCover.LoadAsync(m_SelectedAlbum.GetAlbumCoverURL());
                }
                catch
                {
                    MessageBox.Show(
                        "No pictures in this album.",
                        "Sorry",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation,
                        MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void buttonDisplayAlbum_Click(object sender, EventArgs e)
        {
            AlbumForm albumFormDialogBox = null;

            if (m_FacebookOperator.IsLogedIn)
            {
                albumFormDialogBox = new AlbumForm(m_SelectedAlbum);
                albumFormDialogBox.Text = string.Format("{0} - Album", m_SelectedFriendName);
                albumFormDialogBox.ShowDialog();
            }
        }

        private void linkLabelMyAlbum_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (m_FacebookOperator.IsLogedIn)
            {
                listBoxAlbums.DataSource = m_FacebookOperator.LoggedInUser.GetAlbumsList();
                m_SelectedFriendName = m_FacebookOperator.LoggedInUser.UserInformation.Name;
            }
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            ISearchPostBy searchPost = null;

            // Create a specific search based on what the user has decided.
            if (m_FacebookOperator.IsLogedIn && (checkBoxSearchPostByWord.Checked || checkBoxSearchPostByDate.Checked))
            {
                if (checkBoxSearchPostByWord.Checked)
                {
                    m_ValuesToSearch = new ValuesToSearch(textBoxSearchPostByWord.Text, m_FacebookOperator.LoggedInUser);
                    searchPost = new SearchPostsFactory(eSearchPostBy.SearchByWord);
                }
                else
                {
                    if (checkBoxSearchPostByDate.Checked)
                    {
                        m_ValuesToSearch = new ValuesToSearch(dateTimePickerFrom.Value, dateTimePickerTo.Value, m_FacebookOperator.LoggedInUser);
                        searchPost = new SearchPostsFactory(eSearchPostBy.SearchByDate);
                    }
                    else
                    {
                        if (checkBoxSearchPostByWord.Checked && checkBoxSearchPostByDate.Checked)
                        {
                            m_ValuesToSearch = new ValuesToSearch(textBoxSearchPostByWord.Text, dateTimePickerFrom.Value, dateTimePickerTo.Value, m_FacebookOperator.LoggedInUser);
                            searchPost = new SearchPostsFactory(eSearchPostBy.SearchByWordAndDate);
                        }
                    }
                }

                if (searchPost != null)
                {
                    listBoxFeed.DataSource = StartSearchingPosts(searchPost); // Update the listBox feed with the new data.
                }
            }
        }

        private List<FeedWrapper> StartSearchingPosts(ISearchPostBy i_SearchPostBy)
        {
            ISearchPost postToSearch = i_SearchPostBy.SearchByAction();

            return postToSearch.SearchPost(m_ValuesToSearch);
        }
    }
}